package com.poisonh.unifiedauth.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.adapter.CmVpAdapter;
import com.poisonh.unifiedauth.adapter.PtrrvAdapter;
import com.poisonh.unifiedauth.model.RecordModel;
import com.poisonh.unifiedauth.ui.fragment.CircleFragment;
import com.poisonh.unifiedauth.ui.fragment.LineGraphFragment;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.GsonUtils;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;
import com.poisonh.unifiedauth.widget.CircleIndicator;
import com.poisonh.unifiedauth.widget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * 认证管理
 * Created by PoisonH on 2016/6/29.
 */
public class CertificationManagementActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener
{
    private ViewPager mVpCertificationManagementPager;
    private CircleIndicator mCiCertificationManagementIndicator;
    private CmVpAdapter mCmVpAdapter;
    private List<Fragment> mListFragment;
    private XRecyclerView mRecyclerView;
    private RadioGroup mRadioGroup;
    private RadioButton mRbBtnA;
    private RadioButton mRbBtnB;
    private RadioButton mRbBtnC;
    private PtrrvAdapter mPtrrvAdapter;
    private View mViewHeader;
    //当前页
    private int page = 1;
    //处理类型:0已处理，1未处理，2已失效
    private int type = 0;
    private boolean onLoadMore = false;

    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_certificationmanagement);
        initTitleView();
        initView();
        getAppLoginRecord(page, type);
    }

    /**
     * 初始化视图
     */
    @Override
    protected void initView()
    {
        setTitleTxt("认证管理");
        mRightBtn.setVisibility(View.GONE);
        mLeftBtn.setOnClickListener(this);
        mViewHeader = View.inflate(this, R.layout.layout_loadmore_header, null);
        mVpCertificationManagementPager = (ViewPager) mViewHeader.findViewById(R.id.vp_certificationmanagement_pager);
        mCiCertificationManagementIndicator = (CircleIndicator) mViewHeader.findViewById(R.id.ci_certificationmanagement_indicator);

        mRecyclerView = (XRecyclerView) findViewById(R.id.xrecyclerview);

        mRadioGroup = (RadioGroup) findViewById(R.id.group);
        mRbBtnA = (RadioButton) findViewById(R.id.rb_btn1);
        mRbBtnB = (RadioButton) findViewById(R.id.rb_btn2);
        mRbBtnC = (RadioButton) findViewById(R.id.rb_btn3);
        mRadioGroup.setOnCheckedChangeListener(this);

        mListFragment = new ArrayList<>();
        mListFragment.add(new CircleFragment());
        mListFragment.add(new LineGraphFragment());

        mCmVpAdapter = new CmVpAdapter(getSupportFragmentManager(), mListFragment);
        mVpCertificationManagementPager.setAdapter(mCmVpAdapter);
        mCiCertificationManagementIndicator.setViewPager(mVpCertificationManagementPager);
        mVpCertificationManagementPager.setCurrentItem(0);


        mListFragment = new ArrayList<>();
        mListFragment.add(new CircleFragment());
        mListFragment.add(new LineGraphFragment());

        mCmVpAdapter = new CmVpAdapter(getSupportFragmentManager(), mListFragment);
        mVpCertificationManagementPager.setAdapter(mCmVpAdapter);
        mCiCertificationManagementIndicator.setViewPager(mVpCertificationManagementPager);
        mVpCertificationManagementPager.setCurrentItem(0);

        mPtrrvAdapter = new PtrrvAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        // mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(mPtrrvAdapter);
        mRecyclerView.addHeaderView(mViewHeader);
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener()
        {
            @Override
            public void onRefresh()
            {
                // mRecyclerView.refreshComplete();
            }

            @Override
            public void onLoadMore()
            {
                page += 1;
                onLoadMore = true;
                getAppLoginRecord(page, type);
            }
        });


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        page = 1;
        onLoadMore = false;
        switch (checkedId)
        {
            case R.id.rb_btn1:
                mPtrrvAdapter.cleanData();
                mRecyclerView.setLoadingMoreEnabled(true);
                type = 0;
                getAppLoginRecord(page, type);
                break;
            case R.id.rb_btn2:
                mPtrrvAdapter.cleanData();
                mRecyclerView.setLoadingMoreEnabled(true);
                type = 1;
                getAppLoginRecord(page, type);
                break;
            case R.id.rb_btn3:
                mPtrrvAdapter.cleanData();
                mRecyclerView.setLoadingMoreEnabled(true);
                type = 2;
                getAppLoginRecord(page, type);
                break;
        }
    }

    /**
     * 获取APP授权登录记录
     *
     * @param page
     * @param type
     */
    private void getAppLoginRecord(int page, int type)
    {
        HttpParams params = new HttpParams();
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        params.put("page", page + "");
        params.put("size", "10");
        params.put("type", type + "");
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.GETAPPLOGINRECORD, params,
                new MyHttpsUtils.ResultCallback()
                {
                    @Override
                    public void onSuccess(String response)
                    {
                        List<RecordModel> recordModels = GsonUtils.parseJson(response);
                        Message msg = Message.obtain();
                        msg.obj = recordModels;
                        mHanler.sendMessage(msg);
                        mRecyclerView.loadMoreComplete();
                        mRecyclerView.refreshComplete();
                    }

                    @Override
                    public void onFailure(String failReason)
                    {
                        ToastUtils.showToast(CertificationManagementActivity.this, "获取数据失败");
                        mRecyclerView.loadMoreComplete();
                        mRecyclerView.refreshComplete();
                    }
                });
    }

    Handler mHanler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            List<RecordModel> recordModels = new ArrayList<>();
            if (onLoadMore == false)
            {
                recordModels.clear();
            }
            recordModels = (List<RecordModel>) msg.obj;
            if (recordModels.size() > 0)
            {
                mPtrrvAdapter.setData(recordModels, onLoadMore);
            } else
            {
                ToastUtils.showToast(CertificationManagementActivity.this, "没有更多数据");
                mRecyclerView.setLoadingMoreEnabled(false);
            }
        }
    };

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
        }
    }
}
